package com.tannm.doan.taskmanager.agenda;

import java.util.*;

import com.tannm.doan.taskmanager.TaskManager;
import com.tannm.doan.taskmanager.dataview.DataView;
import com.tannm.doan.taskmanager.dataview.DataViewItem;
import com.tannm.doan.taskmanager.views.ViewWeekDayItem;

import android.graphics.Paint;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class AgendaViewWeek extends AgendaView {
	// fields
	private ArrayList<ViewWeekDayItem> vecDayItems = new ArrayList<ViewWeekDayItem>();
	protected LinearLayout llayParentWeek = null;
	Calendar calWeekStart = Calendar.getInstance();

	// fields
	private Paint mpt = new Paint();

	// methods
	public AgendaViewWeek(TaskManager main) {
		super(main);

		LinearLayout.LayoutParams layParams = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, main.getViewSpaceHeight());

		// create parent content layout for week view (don't know why not works
		// for llayParent
		llayParentWeek = new LinearLayout(main);
		llayParentWeek.setPadding(0, 0, 0, 0);
		llayParentWeek.setOrientation(LinearLayout.HORIZONTAL);
		llayParentWeek.setLayoutParams(layParams);
	}

	@Override
	public int getViewType() {
		return viewMode.WEEK;
	}

	@Override
	public int getViewIndex() {
		return 2;
	}

	private ViewWeekDayItem.OnItemClick mWeekDayItemClick = new ViewWeekDayItem.OnItemClick() {
		public void OnClick(ViewWeekDayItem item) {
			onDayItemClick(item);
		}
	};

	@Override
	public void rebuild() {
		llayParent.removeAllViews();
		llayParentWeek.removeAllViews();

		final int iParentWidth = main.getViewSpaceWidth();
		final int iParentHeight = main.getViewSpaceHeight() - 5;
		int iDefaultWidth = 0;
		int iWidth = 0;

		final int iSpaceWidthTime = ViewWeekDayItem.getSpaceWidthTime(mpt,
				main.prefs.b24HourMode);
		final int iSpaceHeightHeader = ViewWeekDayItem
				.getSpaceHeightHeader(mpt);

		// build 7 days week view
		vecDayItems.clear();
		for (int iDay = 0; iDay < 7; iDay++) {
			ViewWeekDayItem item = new ViewWeekDayItem(main, iSpaceHeightHeader);

			// set event
			item.setItemClick(mWeekDayItemClick);

			// set size
			if (iDay == 0) {
				item.setTimeMargin(iSpaceWidthTime, main.prefs.b24HourMode);
				iDefaultWidth = (iParentWidth - iSpaceWidthTime) / 7;
				iWidth = iDefaultWidth + iSpaceWidthTime;
			} else {
				iWidth = iDefaultWidth;
			}
			item.setSize(iWidth, iParentHeight);

			// add to layout
			vecDayItems.add(item);
			llayParentWeek.addView(item);
		}

		llayParent.addView(llayParentWeek);
	}

	public void onDayItemClick(ViewWeekDayItem item) {
		Calendar calDate = Calendar.getInstance();
		calDate.setTimeInMillis(item.getDate().getTimeInMillis());
		main.setAgendaView(AgendaView.viewMode.DAY, calDate);
	}

	@Override
	public void rebuildViewAppointments(DataView dataView) {
		final boolean bIsViewToday = isViewToday();
		final int iCurrentHour = getTodayCurrentHour();

		calWeekStart.setTimeInMillis(getViewStartDate().getTimeInMillis());
		calWeekStart.setFirstDayOfWeek(main.prefs.iFirstDayOfWeek);

		// setup weekdays data
		Calendar calDateToday = main.getDateToday();
		for (int iDay = 0; iDay < 7; iDay++) {
			ViewWeekDayItem item = vecDayItems.get(iDay);
			item.clearTimeItems();
			item.setDate(calWeekStart, calDateToday);
			item.setCurrentHour((bIsViewToday) ? iCurrentHour : -1);
			calWeekStart.add(Calendar.DAY_OF_YEAR, 1);
		}

		// rebuild view
		for (int i = 0; i < dataView.getRowsCountTotal(); i++) {
			// get data item
			DataViewItem row = dataView.getRow(i, this.getViewType());
			if (row != null) {
				// iterate repeat week days in data item
				for (int iDay = 0; iDay < 7; iDay++) {
					// get item visible date
					if (row.getVisibleDay(iDay)) {
						// get week day item
						ViewWeekDayItem item = vecDayItems.get(iDay);
						item.addTimeItem(row.getStartHour(), row
								.getStartMinute(), row.iDurationInMinutes);
					}
				}

			}
		}

		llayParentWeek.requestLayout();
		llayParentWeek.invalidate();
	}

	@Override
	public void rebuildViewNotes(DataView dataView) {
	}

	@Override
	public void rebuildViewTasks(DataView dataView) {
	}

	@Override
	public void updateTimeFormat() {
		int iParentWidth = main.getViewSpaceWidth();
		int iParentHeight = main.getViewSpaceHeight();
		int iDefaultWidth = 0;
		int iWidth = 0;

		final int iSpaceWidthTime = ViewWeekDayItem.getSpaceWidthTime(mpt,
				main.prefs.b24HourMode);

		for (int iDay = 0; iDay < 7; iDay++) {
			ViewWeekDayItem item = vecDayItems.get(iDay);
			if (iDay == 0) {
				item.setTimeMargin(iSpaceWidthTime, main.prefs.b24HourMode);
				iDefaultWidth = (iParentWidth - iSpaceWidthTime) / 7;
				iWidth = iDefaultWidth + iSpaceWidthTime;
			} else {
				iWidth = iDefaultWidth;
			}
			item.setSize(iWidth, iParentHeight);
		}
	}

}
